<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>权限编辑</title>
    <script src="../../js/jquery.js"></script>
    <script src="../../layui/layui.js"></script>
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <script src="../../js/jquery.cookei.js"></script>
    <script src="../../js/myAjax.js"></script>
    <script src="../../js/utlis/edit.js"></script>
</head>
<body>
<div class="layui-form" style="margin-left: 80px; margin-top: 20px">
    <div type="hidden" id="permissionId"></div>
    <div class="layui-form-item">
        <label class="layui-form-label">权限名</label>
        <div class="layui-input-inline">
            <input type="text" name="title"  required lay-verify="required" id="name" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">类型</label>
        <div class="layui-input-inline">
            <input type="text" name="title" readonly  required lay-verify="required" id="type" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">属性值</label>
        <div class="layui-input-inline">
            <input type="text" name="title" required lay-verify="required" id="value" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">路径</label>
        <div class="layui-input-inline">
            <input type="text" name="title" required lay-verify="required" id="src" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">激活</label>
        <div class="layui-input-block">
            <input type="checkbox" value="1" name="switch" lay-skin="switch">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">创建时间</label>
        <div class="layui-input-inline">
            <input type="text" name="title" readonly required lay-verify="required" id="createTime" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">确认修改</button>
            <button type="reset" onclick="resetEdit('/html/permission/roleEdit.html')" class="layui-btn layui-btn-primary">
                点击重置
            </button>
        </div>
    </div>
</div>
</body>
<script>
    //Demo
    layui.use('form', function(){
        var form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function(){
            // layer.msg(JSON.stringify(data.field));
            let data = {
                id:sessionStorage.getItem("permissionId"),
                name:$("#name").val(),
                value:$("#value").val(),
                src:$("#src").val(),
                type:$("#type").val(),
                status:$('input:checkbox[name="switch"]:checked').val(),
            }
            if (data.status==null){
                data.status=0;
            }
            if (data.type=="菜单"){
                data.type=1;
            }else if (data.type=="操作"){
                data.type=2;
            }
            console.log(data);
            let result = myAjax('/p/permission/edit','post',data);
            if (result.code===200){
                layer.msg("修改成功");
                parent.layer.closeAll();
                parent.location.reload();
            }else {
                layer.msg("系统繁忙！请稍后再试！");
            }
            return false;
        });
    });
</script>
<script>
    let id = sessionStorage.getItem("permissionId");
    findById('/p/permission/findById',id);
    function findById(url,id) {
        let result = myAjax(url,'post',{id:id});
        $("#name").val(result.data.name);
        $("#value").val(result.data.value);
        $("#src").val(result.data.src);
        if (result.data.type===1){
            $("#type").val('菜单');
        }else if(result.data.type===2){
            $("#type").val('操作');
        }
        if (result.data.status === 1) {
            $("input:checkbox[value='1']").attr('checked', 'true');
        }
        $("#createTime").val(result.data.createTime);
    }
</script>
</html>